package com.ruoyi.quartz.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.dao.SysJobLogDAO;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.service.ISysJobLogService;

/**
 * 定时任务调度日志信息 服务层
 * 
 * @author ruoyi
 */
@Service
public class SysJobLogServiceImpl implements ISysJobLogService
{
    @Autowired
    private SysJobLogDAO jobLogDAO;

    /**
     * 获取quartz调度器日志的计划任务
     * 
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog)
    {
        return jobLogDAO.findAll(getSpecification(jobLog));
    }
    
    private Specification<SysJobLog> getSpecification(SysJobLog jobLog){
    	return (root, criteriaQuery, criteriaBuilder) -> {
			List<Predicate> predicateList = new ArrayList<>();
			if (!StringUtils.isEmpty(jobLog.getJobName())) {
				predicateList.add(criteriaBuilder.like(root.get("jobName"),jobLog.getJobName()));
			}
			if (!StringUtils.isEmpty(jobLog.getJobGroup())) {
				predicateList.add(criteriaBuilder.equal(root.get("jobGroup"), jobLog.getJobGroup()));
			}
			if (!StringUtils.isEmpty(jobLog.getStatus())) {
				predicateList.add(criteriaBuilder.equal(root.get("status"), jobLog.getStatus()));
			}
			if (!StringUtils.isEmpty(jobLog.getInvokeTarget())) {
				predicateList.add(criteriaBuilder.like(root.get("invokeTarget"), jobLog.getInvokeTarget()));
			}
			if (!StringUtils.isNull(jobLog.getParams().get("beginTime"))) {
				predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("create_time"), jobLog.getParams().get("beginTime").toString()));
			}
			
			if (!StringUtils.isNull(jobLog.getParams().get("endTime"))) {
				predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("create_time"), jobLog.getParams().get("endTime").toString()));
			}
			
			Predicate[] p = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(p));
		};
    }
    
    

    /**
     * 通过调度任务日志ID查询调度信息
     * 
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId)
    {
        return jobLogDAO.findById(jobLogId).orElse(null);
    }

    /**
     * 新增任务日志
     * 
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog)
    {
    	jobLogDAO.save(jobLog);
    }

    /**
     * 批量删除调度日志信息
     * 
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds)
    {
    	jobLogDAO.deleteAllByIdInBatch(Arrays.asList(logIds));
        return 1;
    }

    /**
     * 删除任务日志
     * 
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId)
    {
    	jobLogDAO.deleteById(jobId);
        return 1;
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog()
    {
        jobLogDAO.deleteAll();
    }
}
